package com.xhwl.logistics.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.query.cent.sdata.AccountQuery;
import com.xhwl.common.query.cent.sdata.StaffQuery;
import com.xhwl.logistics.service.IOwnerInfoService;
import com.xhwl.logistics.service.business.VehicleCommonBusinessService;
import com.xhwl.logistics.service.cent.IAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: lu.Aaron
 * @create: 2021-06-02 09:55
 **/
//@Service
@Slf4j
public class OwnerInfoServiceImpl implements IOwnerInfoService {
    @Autowired
    private IAccountService accountService;

    @Autowired
    private VehicleCommonBusinessService vehicleCommonBusinessService;

    //常量，定义魔法值
    private static final Integer current = 1;
    private static final Integer size = 5;

    /**
     * 暂时提供这个接口给前端，用来获取车主信息，建议前端直接调用主数据那边的接口
     * @param accountQuery
     * @return
     */
    @Override
    public List<AccountDTO> getUserInfo(AccountQuery accountQuery) {

        /**
         * 由于后续项目变为全业态，故修改代码逻辑
         */
        List<StaffDTO> staffDTOS = new LinkedList<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(size);
        if(accountQuery!=null){
            List<Integer> projectIdList = accountQuery.getProjectIdList();
            if(!projectIdList.isEmpty()){
                StaffQuery staffQuery = new StaffQuery();
                staffQuery.setProjectId(projectIdList);
                staffQuery.setNameAndEntName(accountQuery.getLikeName());
                //获取符合查询条件的数量
                Page<StaffDTO> staffDTOPageCount = accountService.page(staffQuery, page);
                page.setCurrent(current);
                page.setSize(staffDTOPageCount.getTotal());
                Page<StaffDTO> staffDTOPage = accountService.page(staffQuery, page);
                staffDTOS = staffDTOPage.getRecords();
            }
        }
        return staffDTOS.stream().map(this::staffDTOtoAccountDTO).collect(Collectors.toList());


//        //调用主数据，获取人员数据信息
//        Long size = accountQuery.getSize();
//        Long current = accountQuery.getCurrent();
//        if (Objects.isNull(size)) {
//            throw new BusinessException("分页参数必传：size");
//        }
//        if (Objects.isNull(current)) {
//            throw new BusinessException("分页参数必传：current");
//        }
//        Page page = new Page();
//        page.setCurrent(current);
//        page.setSize(size);
//        Page<AccountDTO> pageList = accountService.getPageList(page, accountQuery);
//        List<AccountDTO> records = pageList.getRecords();
//        log.info("获取人员数据信息总条数: {}", records.size());
//        //根据业态过滤
//        Short industryId = accountQuery.getIndustryId();
//        List<AccountDTO> list = new ArrayList<>();
//        records.forEach(r->{
//            String roleName = r.getRoleName();
//            List<String> roleNames = Arrays.asList(roleName.split("、"));
//            //校园业态车主人员
//            if (IndustryEnum.SCH.getId()==industryId) {
//
//                if (roleNames.contains(InitializeRoleEnum.TEACHER.name)||roleNames.contains(InitializeRoleEnum.PARENT.name)||roleNames.contains(InitializeRoleEnum.LOGISTICS.name)||roleNames.contains(InitializeRoleEnum.STUDENT.name)) {
//                    list.add(r);
//                }
//            }
//            //住宅业态车主人员
//            if (IndustryEnum.RSD.getId()==industryId) {
//                if (roleNames.contains(InitializeRoleEnum.PROPERTY_OWNER_FAMILY.name)||roleNames.contains(InitializeRoleEnum.PROPERTY_OWNER_TENANT.name)||roleNames.contains(InitializeRoleEnum.PROPERTY_OWNER.name)) {
//                    list.add(r);
//                }
//            }
//            //商写业态车主人员
//            if (IndustryEnum.BUZ.getId()==industryId) {
//                if (roleNames.contains(InitializeRoleEnum.COMPANY_ADMIN.name)||roleNames.contains(InitializeRoleEnum.COMPANY_COMMON_STAFF.name)) {
//                    list.add(r);
//                }
//            }
//            //制造园区车主人员
//            if(IndustryEnum.PRD.getId()==industryId){
//                for (Integer projectId : r.getProjectIds()) {
//                    Integer integer = accountQuery.getProjectIdList().stream().filter(s -> {
//                        if (StringUtils.equals(s.toString(), projectId.toString())) {
//                            return true;
//                        } else {
//                            return false;
//                        }
//                    }).findAny().orElse(null);
//                    if(integer!=null){
//                        list.add(r);
//                        break;
//                    }
//                }
//            }
//
//
//        });
//        //由于主数据传过来时存在脏数据，除去脏数据
//        List<AccountDTO> accountDTOS = new ArrayList<>();
//        for (AccountDTO accountDTO : list) {
//            AccountDTO accountDTO1 = accountDTOS.stream().filter(s -> {
//                if (StringUtils.equals(s.getId().toString(), accountDTO.getId().toString())) {
//                    return true;
//                } else {
//                    return false;
//                }
//            }).findAny().orElse(null);
//            if (accountDTO1 == null) {
//                accountDTOS.add(accountDTO);
//            }
//        }
//
//        return accountDTOS;
    }

    /**
     * 将StaffDTO转换为AccountDTO，只转换了部分(员工账号id，员工姓名，员工电话，角色ids），可以优化
     */
    private AccountDTO staffDTOtoAccountDTO(StaffDTO staffDTO){
        log.info("角色信息为："+staffDTO.getRoleIds());
        AccountDTO accountDTO = new AccountDTO();
        if(staffDTO != null){
            accountDTO.setId(staffDTO.getAccountId());
            accountDTO.setName(staffDTO.getName());
            accountDTO.setPhone(staffDTO.getPhone());
            accountDTO.setRoleIds(staffDTO.getRoleIds());
        }
        return accountDTO;
    }
}

